package people.paper.tv.activity.home.modual.sptt.view.impl;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.skyworth.ui.api.SkyWithBGLoadingView;
import com.skyworth.util.Util;

import java.util.List;

import people.paper.tv.R;
import people.paper.tv.activity.home.HomeActivity;
import people.paper.tv.activity.home.HomeFragment;
import people.paper.tv.activity.home.modual.sptt.view.data.VideoData;
import people.paper.tv.activity.home.modual.ywyl.view.impl.IViewCallBack;
import people.paper.tv.activity.home.view.impl.HomeViewImpl;
import people.paper.tv.utils.ui.Common;
import people.paper.tv.utils.ui.ServerErrorLayout;

public class HeadlineLayout extends FrameLayout implements View.OnKeyListener/*, View.OnClickListener*/ {
    FrameLayout contentLayout;
    public HeadlineListView listView;
    VideoLayout videoLayout;
    boolean isBigVideo = false;
    boolean isHorizontal = true;
    ISPTTViewCallBack callBack;
    View focus;
    boolean listVisible = false;
    final int listId = 999;
    int curPosition = -1;
    int width, height, left, top, fullW, fullH;

    public HeadlineLayout(@NonNull Context context) {
        super(context);
        FrameLayout content = new FrameLayout(context);
        addView(content, Util.Div(1920), Util.Div(1080));

        contentLayout = new FrameLayout(context);
        LayoutParams params = new LayoutParams(Util.Div(1746), Util.Div(840));
        params.leftMargin = Util.Div(87);
        params.topMargin = Util.Div(160);
        addView(contentLayout, params);

        listView = new HeadlineListView(getContext());
        listView.setParentLayout(this);
        listView.setId(listId);
        contentLayout.addView(listView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        videoLayout = new VideoLayout(context);
        LayoutParams videoParams = new LayoutParams(Util.Div(1049), Util.Div(590));
        videoParams.leftMargin = Util.Div(754);
        videoParams.topMargin = Util.Div(305);
        addView(videoLayout, videoParams);
        videoLayout.setParentView(this);
//        videoLayout.setOnClickListener(this);
//        videoLayout.setOnKeyListener(this);

        focus = new View(context);
        focus.setFocusable(true);
        focus.setFocusableInTouchMode(true);
        focus.setVisibility(GONE);
        addView(focus);
        focus.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(listVisible && event.getAction() == KeyEvent.ACTION_DOWN){
                    if(keyCode == KeyEvent.KEYCODE_BACK){
                        videoLayout.setListVisible(false);
                        setListVisible(false);
                    }
                }
                return true;
            }
        });
    }

    public boolean setLayoutFocus(){
        if(listView != null)
            return listView.changeFocus();
        return false;
    }

    public void setOrientation(boolean horizontal){
        isHorizontal = horizontal;
        if(isHorizontal){
            fullW = Util.Div(1920);
            fullH = Util.Div(1080);
            width = Util.Div(1049);
            height = Util.Div(590);
            left = Util.Div(754);
            top = Util.Div(305);
        }else {
            fullW = Util.Div(607);
            fullH = Util.Div(1080);
            width = Util.Div(450);
            height = Util.Div(800);
            left = Util.Div(1053);
            top = Util.Div(180);
        }
        refreshVideo();
        videoLayout.setOrientation(isHorizontal);
    }

    public void refreshVideo(){
        if(isBigVideo){
            LayoutParams videoParams = (LayoutParams) videoLayout.getLayoutParams();
            videoParams.width = Util.Div(1920);
            videoParams.height = Util.Div(1080);
            videoParams.leftMargin = 0;
            videoParams.topMargin = 0;
            videoLayout.setLayoutParams(videoParams);
        }else {
            LayoutParams videoParams = (LayoutParams) videoLayout.getLayoutParams();
            videoParams.width = width;
            videoParams.height = height;
            videoParams.leftMargin = left;
            videoParams.topMargin = top;
            videoLayout.setLayoutParams(videoParams);
        }
    }

    public void setParentViewCallback(ISPTTViewCallBack c) {
        this.callBack = c;
        listView.setParentViewCallback(c);
        videoLayout.setParentViewCallback(c);
    }

    public void setVideoFocus(){
        videoLayout.requestFocus();
    }

    public void refresh(int page, List<VideoData> datas) {
        Log.i("SPTTViewImpl onNewsLoad", "SPTTViewImpl getNextPageData:"+page+"; "+datas);
        listView.refresh(page, datas);
    }

    public void refreshCurPlayId(int position){
        curPosition = position;
        listView.refreshCurPlayId(position);
    }

    void addVideoPlayer(View video, boolean isHorizontal) {
        setOrientation(isHorizontal);
        videoLayout.addVideoPlayer(video);
    }

    void clearVideoPlayer() {
        videoLayout.clearVideoPlayer();
    }

    public void refreshDuration(long total, long duration){
        videoLayout.refreshDuration(total, duration);
    }

    public void setLoading(boolean isLoading){
        Log.i("SPTTViewImpl setLoading", "SPTTViewImpl setLoading:"+isLoading);
        if(isLoading){
            videoLayout.showLoading();
        }else {
            videoLayout.hideLoading();
        }
    }

    public void setPlay(boolean isPlay){
        if(listView != null)
            listView.setPlay(isPlay, -1);
    }

    public boolean changeFocus(boolean isLeft) {
        if(listView != null){
            return listView.changeFocus();
        }
        return false;
    }

//    @Override
//    public void onClick(View v) {
//        if(!isBigVideo){
////            refreshFullVideo(true);
//            videoLayout.refreshFullVideo(true);
//        }
//    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Log.d("Headline", "--- getFocus ---"+isBigVideo+"； "+keyCode + "; "+(v instanceof HeadlineItemView));
        if(isBigVideo && v instanceof HeadlineItemView){
            if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){
                videoLayout.setListVisible(false);
                setListVisible(false);
                return true;
            }else {
                Log.d("Headline", "--- onKey ---"+isBigVideo+"；false: "+keyCode);
                return false;
            }
        }
        return false;
    }

    public void setListVisible(boolean visible){
        listVisible = true;
        if(visible){
            listView.setVisibility(VISIBLE);
            if(!listView.getFocus()){
                focus.setVisibility(VISIBLE);
                focus.requestFocus();
            }
        }else {
            listView.setVisibility(GONE);
        }
    }

    public boolean setListFocus(){
        return listView.getFocus();
    }

   public void exitFullScreen(){
       Log.i("onNewsLoad", "SPTTViewImpl exitFullScreen:"+curPosition+"; "+listView.getTotal());
        if(curPosition == listView.getTotal()-1){
            refreshFullVideo(false);
            videoLayout.refreshFullVideo(false);
        }
    }

    public void refreshFullVideo(boolean isFull){
        if(isBigVideo == isFull)
            return;
        isBigVideo = isFull;
        refreshVideo();

        if(isFull){
            contentLayout.removeView(listView);
            listView.setMode(true);
            videoLayout.addView(listView, new ViewGroup.LayoutParams(
                    Util.Div(636), ViewGroup.LayoutParams.MATCH_PARENT));
            listView.setVisibility(GONE);
        }else {
            videoLayout.removeView(listView);
            contentLayout.addView(listView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            listView.setMode(false);
            listView.setVisibility(VISIBLE);
        }

        /*focus.setVisibility(VISIBLE);
        focus.requestFocus();
        ViewGroup group = (ViewGroup) (((Activity) getContext()).findViewById(android.R.id.content));
//        ViewGroup group = (ViewGroup) (((Activity) getContext()).findViewById(HomeViewImpl.FRAGMENT_ID));
        if(isFull){
            removeView(videoLayout);
            group.addView(videoLayout, new ViewGroup.LayoutParams(
                    Util.Div(1920), Util.Div(1072)));
            videoLayout.bringToFront();

            contentLayout.removeView(listView);
            listView.setMode(true);
            group.addView(listView, new ViewGroup.LayoutParams(
                    Util.Div(636), ViewGroup.LayoutParams.MATCH_PARENT));
            listView.setVisibility(GONE);
        }else {
            group.removeView(videoLayout);
            LayoutParams videoParams = new LayoutParams(width, height);
            videoParams.leftMargin = left;
            videoParams.topMargin = top;
            addView(videoLayout, videoParams);
            videoLayout.requestFocus();

            group.removeView(listView);
            contentLayout.addView(listView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            listView.setMode(false);
            listView.setVisibility(VISIBLE);
        }
        videoLayout.refreshFullVideo(isFull);
        focus.setVisibility(GONE);*/
    }

    public void resume() {
    }

    public void pause() {
        if(Common.CurTab == HomeActivity.SPTT_INDEX){
            refreshFullVideo(false);
            videoLayout.refreshFullVideo(false);
        }
    }

    public void destroy() {

    }
}
